#!/usr/bin/env python
# -*- coding:utf-8 -*-

import math

from app import db
from controller.accuseInfoController import AccuseInfoController
from models.accuseInfoModel import AccuseInfo
from models.userInfoModel import UserInfo
from models.passageInfoModel import PassageInfo
from utils import commons
from utils.response_code import RET, error_map_EN
from utils.loggings import loggings
from utils.validate_operate import verify_user_role
from sqlalchemy.orm import aliased


class AccuseInfoService(AccuseInfoController):
    @classmethod
    def joint_query(cls, **kwargs):
        try:
            filter_list = []
            filter_list.append(cls.IsDelete == 0)
            if kwargs.get('AutoID'):
                filter_list.append(cls.AutoID == kwargs.get('AutoID'))
            if kwargs.get('AUserID'):
                filter_list.append(cls.AUserID == kwargs.get('AUserID'))
            if kwargs.get('PassageID'):
                filter_list.append(cls.PassageID == kwargs.get('PassageID'))
            if kwargs.get('AccuseContent'):
                filter_list.append(cls.AccuseContent == kwargs.get('AccuseContent'))
            if kwargs.get('IsDelete'):
                filter_list.append(cls.IsDelete == kwargs.get('IsDelete'))
            if kwargs.get('CreateTime'):
                filter_list.append(cls.CreateTime == kwargs.get('CreateTime'))
            if kwargs.get('ModifyTime'):
                filter_list.append(cls.ModifyTime == kwargs.get('ModifyTime'))

            page = int(kwargs.get('Page', 1))
            size = int(kwargs.get('Size', 10))

            accuseInfo_info = db.session.query(AccuseInfo.AutoID, AccuseInfo.AUserID, AccuseInfo.PassageID,
                                               AccuseInfo.AccuseContent, AccuseInfo.IsDelete, AccuseInfo.CreateTime,
                                               AccuseInfo.ModifyTime).filter(*filter_list)

            count = accuseInfo_info.count()
            pages = math.ceil(count / size)
            accuseInfo_info = accuseInfo_info.limit(size).offset((page - 1) * size).all()

            if not accuseInfo_info:
                return {'code': RET.NODATA, 'message': error_map_EN[RET.NODATA], 'error': 'No data to update'}

            # 处理返回的数据
            results = commons.query_to_dict(accuseInfo_info)
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'totalCount': count, 'totalPage': pages,
                    'data': results}

        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'error': str(e)}
        finally:
            db.session.close()

    @classmethod
    def getaccuseinfoV1(cls, **kwargs):
        try:
            filter_list = [cls.IsDelete == 0, PassageInfo.IsDelete == 0]
            page = int(kwargs.get('Page', 1))
            size = int(kwargs.get('Size', 10))
            Author = aliased(UserInfo, name='Author')
            accuseInfo_info = db.session.query(AccuseInfo.AUserID, UserInfo.UserNickName, UserInfo.FaceImg,
                                               PassageInfo.PostTitle,PassageInfo.AutoID.label('PassageID'),
                                               PassageInfo.PostContent.label('Passagecontent'),
                                               Author.UserID.label('AuthorID'),
                                               Author.UserNickName.label('AuthorNickName'),
                                               Author.FaceImg.label('AuthorFaceImg'), AccuseInfo.PassageID,
                                               AccuseInfo.AccuseContent, AccuseInfo.IsDelete, AccuseInfo.CreateTime,
                                               AccuseInfo.ModifyTime).join(PassageInfo,
                                                                           PassageInfo.AutoID == AccuseInfo.PassageID).join(Author,Author.UserID == PassageInfo.PostAuthorID).join(
                UserInfo, UserInfo.UserID == AccuseInfo.AUserID).filter(*filter_list)

            count = accuseInfo_info.count()
            pages = math.ceil(count / size)
            accuseInfo_info = accuseInfo_info.limit(size).offset((page - 1) * size).all()
            print(accuseInfo_info)
            if not accuseInfo_info:
                return {'code': RET.NODATA, 'message': error_map_EN[RET.NODATA], 'error': 'No data to update'}

            # 处理返回的数据
            results = commons.query_to_dict(accuseInfo_info)
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'totalCount': count, 'totalPage': pages,
                    'data': results}

        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'error': str(e)}
        finally:
            db.session.close()

    @classmethod
    @verify_user_role([1, ])
    def getaccuseinfoV2(cls, **kwargs):
        try:
            filter_list = [cls.IsDelete == 0, PassageInfo.IsDelete == 0]
            page = int(kwargs.get('Page', 1))
            size = int(kwargs.get('Size', 10))
            Author = aliased(UserInfo, name='Author')
            accuseInfo_info = db.session.query(AccuseInfo.AUserID, UserInfo.UserNickName, UserInfo.FaceImg,
                                               PassageInfo.PostTitle, PassageInfo.AutoID.label('PassageID'),
                                               PassageInfo.PostContent.label('Passagecontent'),
                                               Author.UserID.label('AuthorID'),
                                               Author.UserNickName.label('AuthorNickName'),
                                               Author.FaceImg.label('AuthorFaceImg'), AccuseInfo.PassageID,
                                               AccuseInfo.AccuseContent, AccuseInfo.IsDelete, AccuseInfo.CreateTime,
                                               AccuseInfo.ModifyTime).join(PassageInfo,
                                                                           PassageInfo.AutoID == AccuseInfo.PassageID).join(
                Author, Author.UserID == PassageInfo.PostAuthorID).join(
                UserInfo, UserInfo.UserID == AccuseInfo.AUserID).filter(*filter_list)

            count = accuseInfo_info.count()
            pages = math.ceil(count / size)
            accuseInfo_info = accuseInfo_info.limit(size).offset((page - 1) * size).all()
            print(accuseInfo_info)
            if not accuseInfo_info:
                return {'code': RET.NODATA, 'message': error_map_EN[RET.NODATA], 'error': 'No data to update'}

            # 处理返回的数据
            results = commons.query_to_dict(accuseInfo_info)
            return {'code': RET.OK, 'message': error_map_EN[RET.OK], 'totalCount': count, 'totalPage': pages,
                    'data': results}

        except Exception as e:
            loggings.exception(1, e)
            return {'code': RET.DBERR, 'message': error_map_EN[RET.DBERR], 'error': str(e)}
        finally:
            db.session.close()